package br.unipar.bancodedados.manager.impl;

import br.unipar.bancodedados.entidade.Categoria;
import br.unipar.bancodedados.manager.CategoriaManager;
import static com.google.code.liquidform.LiquidForm.*;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import org.springframework.transaction.annotation.Transactional;

public class JpaCategoriaManagerImpl implements CategoriaManager {

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    @Transactional
    public void salvar(Categoria categoria) {
        entityManager.merge(categoria);
    }

    @Override
    @Transactional(readOnly=true)
    public List<Categoria> listar() {
        Categoria c = alias(Categoria.class, "c");
        String qlString = select(c).from(Categoria.class).as(c).toString();

        System.out.println("HQL >>>>>>>>> " + qlString);

        try {
            return entityManager.createQuery(qlString).getResultList();
        } catch (NoResultException e) {
            return new ArrayList<Categoria>();
        }
    }

}
